package com.jacky.spider;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class SpiderPageProcessor implements PageProcessor {
    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(1000)
            .setTimeOut(10000)
            .setDomain("http://www.cninfo.com.cn/new/index");

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        //获取所有公告链接
        List<String> noticeLinks = page.getHtml().links().regex("http://www\\.cninfo\\.com\\.cn/new/commonUrl\\?url=disclosure/list/notice#[a-z]+").all();
        for (int i = 0; i < noticeLinks.size(); i++) {
            noticeLinks.set(i,noticeLinks.get(i) + "%2Fimportant") ;
        }
        page.addTargetRequests(noticeLinks);

//        page.putField("all", page.getHtml().xpath("//div[@class='el-table__body-wrapper is-scrolling-none']"));
//        page.putField("all2", page.getHtml());

//        page.putField("url", page.getHtml().xpath("//*[@id=\"pane-important\"]/div[1]/div/div[3]/table/tbody/tr[1]/td[4]/div/a[1]/span[1]/text()"));
        page.putField("title", page.getHtml().xpath("//div[@class='el-table__body-wrapper is-scrolling-none']/table/tbody/tr/td[4]"));
//        page.putField("content", page.getHtml().$("div.content").toString());
//        page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
        if (page.getResultItems().get("title")==null){
            page.setSkip(true);
        }
    }

    public static void main(String[] args) {
        Spider.create(new SpiderPageProcessor())
                .addUrl("http://www.cninfo.com.cn/new/disclosure/stock?orgId=gssh0600519&stockCode=600519#latestAnnouncement")
                .addPipeline(new ConsolePipeline())
                .run();
    }

}
